// miniprogram/pages/work_log/List.js
var util = require('../util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    load: true,//加载
    page: 1,
    list: [],
    shareshow: false,
    btn: "继续加载"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '正在加载数据',
    })
    var now = new Date().Format("yyyy-MM-dd");
    this.setData({ nowdate: now });
    this.GetList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  BackBtn: function () {
    wx.navigateBack(
    )
  },
  Loadpage: function () {

    var newpage = this.data.page + 1
    this.setData({ page: newpage });
    this.GetList();
  },
  GetList: function () {
    var that = this, data = '', data_name = 'K_W_Record', data_json = '"page": ' + this.data.page + ',"count": 5,';
    data_json = data_json + '"TypeName": "store","Openid": "' + getApp().globalData.openid + '",';
    data_json = data_json + '"@column": "id,Name,Rid,Pid,CreateDate,attributeValue0","@order": "CreateDate-"';
    data = '{"[]":{"query":2,"' + data_name + '": { ' + data_json + '}},"total@":"/[]/total"}';    
    wx.cloud.callFunction({
      name: 'Get_fun',
      data: {
        data: data
      },
      success: res => {
        //console.log(res.result);
        let obj = JSON.parse(res.result);
        that.setData({ list: this.data.list.concat(obj["[]"]) });
        //sl=当前记录数、dsl=总记录数
        let sl = that.data.list.length, dsl = obj["total"];
        if (sl == dsl) {
          that.setData({ shareshow: true, btn: "加载完成" })
        }
        wx.hideLoading({
          complete: (res) => { },
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err);
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
      }
    })
  },
  ShowBtn: function (e) {
    wx.navigateTo({
      url: 'show?id=' + e.currentTarget.dataset.id,
    })
  },
  EdiBtn: function (e) {
    wx.showModal({
      title: '修改提示',
      content: '是否修改：' + e.currentTarget.dataset.name,
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: 'show?edit=true&id=' + e.currentTarget.dataset.id,
          })
        }
      }
    })
  },
  DelBtn: function (e) {
    var that = this, id = e.currentTarget.dataset.id, name = e.currentTarget.dataset.name;
    let data = '{"K_W_Record": { "id": ' + id + '}}';
    console.log(data);
    wx.showModal({
      title: '删除提示',
      content: '是否删除，' + name,
      success(res) {
        if (res.confirm) {
          util.Yun_del(data);
          wx.showToast({
            icon: 'none',
            title: '正在删除。。',
            duration: 2000,
            success: function () {
              that.setData({
                page: 1,
                list: [],
                shareshow: false,
                btn: "继续加载"
              });
              setTimeout(function () {
                that.onLoad()
              }, 2000) //延迟时间
            }
          })
        }
      }
    });


  }
})